package com.atguigu.binarysearchnorecursion;


public class BinarySearchNoRecur {

	public static void main(String[] args) {
	
		int[] arr = {1,3, 8, 10, 11, 67, 100};
		int index = binarySearch1(arr, 67);
		System.out.println("index=" + index);//
	}
	
	
    // 二分查找树非递归算法
	public static int binarySearch1(int[] arr, int target)
	{
        int left=0;
		int right=arr.length-1;
		int mid=arr.length/2;
		while(left<=right)
		{
			if(target>arr[mid])
			{
				left=mid+1;

			} else if(target<arr[mid]){
				right=mid-1;
			} else {
				return mid;
			}
			mid=(left+right)/2;
		}
		return -1;
	}
}
